// pages/Advertisement/Advertisement.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    is_Home:true,
    image:"tw_32.png"
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      var that = this
      //轮播数据请求
      that.advterObj()
      that.GamesObj()
      wx.request({
          url: 'https://caige1.sanliwenhua.com/program/column?app=9',
          method: "get",
          success: function (res) {
              if (res.data.code = 200) {
                  console.log(res.data)
                  var  data= res.data.data;
                  var advList = []
                  for(var i=0;i<data.length;i++){
                      if(data[i].type=="adv_List"){
                          advList.push(data[i])
                      }
                  }
                  that.setData({
                      advList:advList,
                      image:"tw_32.gif"
                  })
                  if(app.globalData.video_complete==0){
                      app.globalData.timer = setTimeout(function(){
                          that.setData({
                              is_Home:false,
                              image:"tw_31.png"
                          })
                          that.NumberAdvterObj()
                      }.bind(this),5000)
                  }else{
                      that.setData({
                          is_Home:false,
                          image:"tw_31.png"
                      })
                  }
              }
          }
      })
  },
  blackHome:function () {
    var that = this
        wx.showModal({
            title: '提示',
            content: '您确定要离开这个页面吗',
            success: function(res) {
                if (res.confirm) {
                    clearTimeout(app.globalData.timer);
                    wx.switchTab({
                        url: '/pages/index/index'
                    })
                    //console.log('用户点击确定')
                } else if (res.cancel) {
                    console.log('用户点击取消')
                }
            }
        })
    },
  //用户访问小程序接口
  userApp:function (e) {
        var that = this
        if(app.globalData.userInfo){
            wx.request({
                url: 'https://caige1.sanliwenhua.com/program/user_app?app=9',
                method: "post",
                data:{
                    x_token:app.globalData.userInfo.token,
                    appid: e.currentTarget.dataset.appid,
                },
                success: function (res) {
                    console.log(res)
                    if(res.data.code == 200){
                        /*wx.showToast({
                            title: "成功了录取记录",
                            icon:"none",
                            duration: 2000
                        })*/
                        that.NumberGamesObj(e.currentTarget.dataset.appid)
                    }
                },
                fail:function (res) {

                }
            })
        }
    },
  //玩游戏数量处理
  GamesObj:function () {
        var that = this
        var myTime = new Date();
        var today = myTime.getDate();
        wx.getStorage({
            key: 'NumberGames',
            success: function (res) {
                var NumberGames = JSON.parse(res.data)
                //console.log('ChallengeObj', ChallengeObj)
                if (today != NumberGames.day) {
                    NumberGames = {
                        day: today,
                        GamesTime: 0,
                        GamesAppid: []
                    }
                }
                wx.setStorage({
                    key: 'NumberGames',
                    data: JSON.stringify(NumberGames)
                })
                app.globalData.NumberGames = NumberGames
                that.setData({
                    GamesTime:NumberGames.GamesTime
                })

            }, fail: function (res) {
                //console.log(res)
                var NumberGames = {
                    day: today,
                    GamesTime: 0,
                    GamesAppid: []
                }
                wx.setStorage({
                    key: 'NumberGames',
                    data: JSON.stringify(NumberGames)
                })
                app.globalData.NumberGames = NumberGames
                that.setData({
                    GamesTime:NumberGames.GamesTime
                })
            }
        })

    },
  NumberGamesObj:function (e) {
        var that = this
        var myTime = new Date();
        var today = myTime.getDate();
        wx.getStorage({
            key: 'NumberGames',
            success: function (res) {
                var NumberGames = JSON.parse(res.data)
                //console.log('ChallengeObj', ChallengeObj)
                var GamesAppid = NumberGames.GamesAppid;
                if (today != NumberGames.day){
                    NumberGames = {
                        day: today,
                        GamesTime: 0,
                        GamesAppid: []
                    }
                }else{
                    if(GamesAppid.length>0){
                        for(var i=0;i<GamesAppid.length;i++){
                            if(NumberGames.GamesAppid[i]==e){
                                NumberGames = {
                                    day: today,
                                    GamesTime: NumberGames.GamesTime,
                                    GamesAppid: GamesAppid
                                }
                                break;
                            }else {
                                if(i+1==GamesAppid.length){
                                    GamesAppid.push(e)
                                    NumberGames = {
                                        day: today,
                                        GamesTime: NumberGames.GamesTime+1,
                                        GamesAppid: GamesAppid
                                    }
                                }
                            }
                        }
                    }else{
                        GamesAppid.push(e)
                        NumberGames = {
                            day: today,
                            GamesTime: NumberGames.GamesTime+1,
                            GamesAppid: GamesAppid
                        }
                    }
                }
                wx.setStorage({
                    key: 'NumberGames',
                    data: JSON.stringify(NumberGames)
                })
                app.globalData.NumberGames = NumberGames
                that.setData({
                    GamesTime:NumberGames.GamesTime
                })

            }, fail: function (res) {
                //console.log(res)
                var NumberGames = {
                    day: today,
                    GamesTime: 0,
                    GamesAppid:[]
                }
                wx.setStorage({
                    key: 'NumberGames',
                    data: JSON.stringify(NumberGames)
                })
                app.globalData.NumberGames = NumberGames
                that.setData({
                    GamesTime:NumberGames.GamesTime
                })
            }
        })

    },
  //任务的完成请求
  taskButton:function (e) {
        var that = this
        if(app.globalData.userInfo){
            wx.request({
                url: 'https://caige1.sanliwenhua.com/program/task/complete?app=9',
                method: "post",
                data:{
                    x_token:app.globalData.userInfo.token,
                    task_id:e
                },
                success: function (res) {
                    console.log(res.data)
                }
            })
        }
    },
  //观看广告处理
  advterObj:function () {
        var that = this
        var myTime = new Date();
        var today = myTime.getDate();
        wx.getStorage({
            key: 'NumberAdvter',
            success: function (res) {
                var NumberAdvter = JSON.parse(res.data)
                //console.log('ChallengeObj', ChallengeObj)
                if (today != NumberAdvter.day) {
                    NumberAdvter = {
                        day: today,
                        videoTime: 0,
                    }
                }
                wx.setStorage({
                    key: 'NumberAdvter',
                    data: JSON.stringify(NumberAdvter)
                })
                app.globalData.NumberAdvter = NumberAdvter
                that.setData({
                    videoTime:NumberAdvter.videoTime
                })

            }, fail: function (res) {
                //console.log(res)
                var NumberAdvter = {
                    day: today,
                    videoTime: 0,
                }
                wx.setStorage({
                    key: 'NumberAdvter',
                    data: JSON.stringify(NumberAdvter)
                })
                app.globalData.NumberAdvter = NumberAdvter
                that.setData({
                    videoTime:NumberAdvter.videoTime
                })
            }
        })

    },
  NumberAdvterObj:function () {
        var that = this
        var myTime = new Date();
        var today = myTime.getDate();
        wx.getStorage({
            key: 'NumberAdvter',
            success: function (res) {
                var NumberAdvter = JSON.parse(res.data)
                //console.log('ChallengeObj', ChallengeObj)
                if (today != NumberAdvter.day) {
                    NumberAdvter = {
                        day: today,
                        videoTime: 0,
                    }
                }else{
                    NumberAdvter = {
                        day: today,
                        videoTime: NumberAdvter.videoTime+1,
                    }
                }
                wx.setStorage({
                    key: 'NumberAdvter',
                    data: JSON.stringify(NumberAdvter)
                })
                app.globalData.NumberAdvter = NumberAdvter
                that.setData({
                    videoTime:NumberAdvter.videoTime
                })

            }, fail: function (res) {
                //console.log(res)
                var NumberAdvter = {
                    day: today,
                    videoTime: 0,
                }
                wx.setStorage({
                    key: 'NumberAdvter',
                    data: JSON.stringify(NumberAdvter)
                })
                app.globalData.NumberAdvter = NumberAdvter
                that.setData({
                    videoTime:NumberAdvter.videoTime
                })
            }
        })
    },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
      // var that = this;
      // clearTimeout(app.globalData.timer);
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
      // var that = this;
      // clearTimeout(app.globalData.timer);
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
      var that = this
      if (res.from === 'button') {

      }
      return {
          title: '小姐姐们发现了一个超级好玩的！',
          path: 'pages/index/index',
          imageUrl:'http://pbkva7be7.bkt.clouddn.com/image/tw_35.png',
          success: function (res) {
              wx.showToast({
                  title: '分享成功!',
                  icon: 'success',
                  duration: 2000
              })

          },fail: function (res) {
              console.log(res)
              wx.showToast({
                  title: '分享失败！',
                  image: '/image/errIcon.png',
                  duration: 2000
              })
          }
      }
  }
})